package com.example.demo.service;

import com.example.demo.entity.Status;
import com.example.demo.repository.StatusRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by ajinlong on 2021/7/29.
 */

@Service
public class StatusService {

    @Autowired
    private StatusRepository statusRepository;

    public void test() {
        Status status;
        List<Status> result;
        long count;

        // 测试查找某作者的博文
        status = new Status();
        status.setAuthor("Peter");
        status.setContent("First blog");
        this.statusRepository.save(status);

        result = this.statusRepository.findByAuthor("Peter");
        System.out.println("result size:" + result.size());

        // 测试查找点赞数大于某值的博文
        status = new Status();
        status.setContent("Second blog");
        status.setLikes(10);
        this.statusRepository.save(status);

        result = this.statusRepository.findByLikesGreaterThan(9);
        System.out.println("result size:" + result.size());

        // 测试查找包含某关键字的博文
        status = new Status();
        status.setContent("Third Blog");
        this.statusRepository.save(status);

        result = this.statusRepository.findByContentContainingIgnoreCase("Blog");
        System.out.println("result size:" + result.size());

        // 测试查找某作者在某时间前发布的博文并按发布时间升序排序
        status = new Status();
        status.setAuthor("Peter");
        status.setContent("Fourth blog");
        status.setCreateTime(Timestamp.valueOf("2016-03-29 11:11:01"));
        this.statusRepository.save(status);

        result = this.statusRepository.findByAuthorAndCreateTimeBeforeOrderByCreateTime("Peter",
                new Timestamp(System.currentTimeMillis()));
        System.out.println("result size:" + result.size());

        // 测试查找某月份发布的博文
        status = new Status();
        status.setContent("Fivth blog");
        status.setCreateTime(Timestamp.valueOf("2016-01-29 1:1:01"));
        this.statusRepository.save(status);

        result = this.statusRepository.findStatusInMonth(1);
        System.out.println("result size:" + result.size());

        // 测试删除某作者的博文
        count = this.statusRepository.deleteByAuthor("Peter");
        System.out.println("update rows:" + count);

        // 测试计算博文条数
        count = this.statusRepository.count();
        System.out.println("rows:" + count);
    }
}
